package cn.edu.hbpu.common.interceptor;

import cn.edu.hbpu.pojo.Student;
import cn.edu.hbpu.pojo.Teacher;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author yuwenteng
 * @Date 2017/9/22 10:18
 * @Description 配置拦截器，对登陆信息进行检查
 */
public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String uri = request.getRequestURI();
        if (uri.indexOf("login") >= 0 || uri.indexOf("attend") >= 0) {
            return true;
        } else {
            HttpSession session = request.getSession();
            Student student = (Student) session.getAttribute("stuInfo");
            Teacher teacher = (Teacher) session.getAttribute("teaInfo");
            if (student != null || teacher != null) {
                return true;
            }
        }
        request.getRequestDispatcher("/login/showLoginPage").forward(request, response);
        return false;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
